package cz.vutbr.fit.xzemko01.tfmcbs.validation.validators;

import java.util.Map;

import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.DiagnosticChain;

import cz.vutbr.fit.xzemko01.tfmcbs.model.Manifestation;
import cz.vutbr.fit.xzemko01.tfmcbs.model.Relationship;
import cz.vutbr.fit.xzemko01.tfmcbs.validation.utils.DiagnosticBuilder;

public class ManifestationValidator implements Validator<Manifestation> {

	private static ManifestationValidator manifestationValidator;
	
	private Validator<Relationship> relationshipValidator;
	
	private ManifestationValidator() {
		relationshipValidator = RelationshipValidator.getInstance();
	}
	
	public static Validator<Manifestation> getInstance() {
		if(manifestationValidator == null) {
			manifestationValidator = new ManifestationValidator();
		}
		return manifestationValidator;
	}
	
	@Override
	public boolean validate(Manifestation manifest, DiagnosticChain diagnostics, Map<Object, Object> context) {
		boolean result = relationshipValidator.validate(manifest, diagnostics, context);
		result &= validateManifestationEnds(manifest, diagnostics, context);
		return result;
	}
	
	private boolean validateManifestationEnds(Manifestation manifest, DiagnosticChain diagnostics, Map<Object, Object> context) {
		boolean result = true;		
		if(manifest.getArtifact() == null) {
			Diagnostic diagnostic = new DiagnosticBuilder(manifest)
				.setMessage("Manifestation \"" + manifest.getName() + "\" has not set artifact.").build();
				diagnostics.add(diagnostic);
			result = false;
		}
		if(manifest.getComponent() == null) {
			Diagnostic diagnostic = new DiagnosticBuilder(manifest)
				.setMessage("Manifestation \"" + manifest.getName() + "\" has not set component.").build();
				diagnostics.add(diagnostic);
			result = false;
		}
		return result;
	}
}
